package com.rjwh.dingdong.client.activity.homework.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.adapter.HomeWorkShareListAdapter;
import com.rjwh.dingdong.client.bean.localbean.HomeWorkListItem;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.mvp.presenter.impl.HomeWorkPresenter;
import com.rjwh.dingdong.client.mvp.view.HomeWorkView;
import com.rjwh.dingdong.client.util.UrlUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 王小珂
 * @ClassName HomeWorkFragment
 * @Description
 * @date 2017-1-4
 * @history 2017-1-4 author: description:
 */

public class HomeWorkFragment extends BaseFragment implements HomeWorkView, HomeWorkShareListAdapter.OnShareBtnListener {

    @BindView(R.id.home_work_listview)
    ListView homeWorkListview;
    @BindView(R.id.home_work_mPullRefreshView)
    SwipyRefreshLayout homeWorkMPullRefreshView;
    private String type;
    private int currentPosition = -1;
    private HomeWorkShareListAdapter adapter;

    List<String> strings = new ArrayList<>();

    private HomeWorkPresenter presenter;
    private boolean isInit;
    private int pageNumber;
    private int ageCode;

    public static HomeWorkFragment newInstance(String type) {
        HomeWorkFragment fragment = new HomeWorkFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString("type");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.home_work_list, container, false);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new HomeWorkPresenter(getContext(), this);
        if (getUserVisibleHint() && isVisible() && isInit) {
            doRefresh();//加载数据的方法
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        //判断是否可见 并且是否已经初始化过，以防空指针
        if (isVisibleToUser && isVisible() && isInit) {
            doRefresh();//加载数据的方法
        } else {
            isInit = false;
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isInit = true;//初始化后 将其设置成true  表示已经初始化控件过了
    }

    private void initView() {
        adapter = new HomeWorkShareListAdapter(getContext(), this);
        homeWorkListview.setAdapter(adapter);
        homeWorkListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HomeWorkShareListAdapter adapter = (HomeWorkShareListAdapter) parent.getAdapter();
                currentPosition = (currentPosition == position) ? -1 : position;
                adapter.setSelectPosition(currentPosition);
                adapter.notifyDataSetChanged();
            }
        });
        homeWorkMPullRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNumber = 0;
                        doGetHomeWorkList(true, SwipyRefreshLayoutDirection.TOP);
                        break;
                    case BOTTOM:
                        pageNumber++;
                        doGetHomeWorkList(false, SwipyRefreshLayoutDirection.BOTTOM);
                        break;
                    default:
                        break;
                }
            }
        });
    }


    private void doGetHomeWorkList(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        presenter.doGetHomeWorkList(ageCode + 1, type, pageNumber, isRefresh, direction);
    }

    @Override
    public void setRefreshing(boolean isRefresh) {
        homeWorkMPullRefreshView.setRefreshing(isRefresh);
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        homeWorkMPullRefreshView.setDirection(direction);
    }

    public void setAgeCode(int ageCode) {
        this.ageCode = ageCode;
    }

    @Override
    public void loadData(boolean isRefresh, List<HomeWorkListItem> homeworklist) {
        if (isRefresh) {
            adapter.clearAll();
        }
        adapter.appendToList(homeworklist);
    }

    @Override
    public void loadAssignData(String actionurl) {
        UrlUtil.handelUrl(getContext(), actionurl, true);
    }

    @Override
    public void showLoadDailog() {
        showLoadDialog();
    }

    @Override
    public void hideLoadDailog() {
        dismissLoadDialog();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }

    public void doRefresh() {
        if (homeWorkMPullRefreshView != null) {
            homeWorkMPullRefreshView.doPullRefreshing();
        }
    }

    @Override
    public void onShareBtnClick(String bookid, String actionurl, String operation) {
        switch (operation) {
            case "1":
                //弹框+请求assign+分享
                showShareDailog(bookid);
                break;
            case "2":
                //请求assign+分享
                presenter.doHomeWorkAssign(bookid, type);
                break;
            case "3":
                //分享
                UrlUtil.handelUrl(getContext(), actionurl, true);
                break;
            default:
                break;
        }
    }

    private void showShareDailog(final String bookid) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage("确定分享作业并推送给我的粉丝么？")
                .setNegativeButton("我再想想", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定分享", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        presenter.doHomeWorkAssign(bookid, type);
                        dialog.dismiss();
                    }
                })
                .show();
    }
}
